// 获取tab栏图标的日期；函数能返回对应的年月日数字
function getIconDate(field) {
  const  date = new Date()
  switch(field) {
    case 'day':
      return date.getDate().toString().padStart(2,'0')  //天，不足两位，对其补0
    case 'month':
      return (date.getMonth() + 1).toString().padStart(2, '0')  //月，月份前面+1
    case 'year':
      return date.getFullYear().toString().substr(2) //从第二位开始截取到最后
  }
}

// 获取当前日期
function getNowDate(field) {
  const date = new Date()
  const day = date.getDate();
  const month = date.getMonth() + 1;
  const year = date.getFullYear()
  switch(field) {
    case 'day': 
      return `${year}-${month}-${day}`
    case 'month': 
      return  `${year}-${month}`
    case 'year' :
      return  `${year}`
  }
}

//将日期格式转换成中文格式
function formatChsDate(date, field) {
  let arr = date.split('-');
  switch(field) {
    case 'day':
      return `${arr[0]}年${arr[1]}月${arr[2]}日`
    case 'month': 
      return `${arr[0]}年${arr[1]}月`
    case 'year':
      return `${arr[0]}年`
  }
}

// 将用户输入的日期格式转换成年-月-日
function formateUserDate(date) {
  switch(date.length) {
    case 8: 
      let year = date.substr(0, 4);
      let month = Number(date.substr(4, 2));  //02 => 2
      let day = Number(date.substr(6, 2));
      return `${year}-${month}-${day}`
    case 6:
      year = date.substr(0, 4)
      month = Number(date.substr(4, 2));
      return `${year}-${month}`
    default:
      return date
  }
}

export {
  getIconDate, 
  getNowDate,
  formatChsDate,
  formateUserDate
}